package com.android.nearshop.app.adapter.listadapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.android.nearshop.R;
import com.android.nearshop.app.widget.LoadableView.IdisplayNetErrAndRefreshView;
import com.android.nearshop.app.widget.PullToRefreshListView;
import com.android.nearshop.io.protocol.in.ShopDisCountListAction;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-31
 * @see : TODO
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class MemFlowViewAdapter extends ArrayListAdapter<ShopDisCountListAction>
{
    private View[] mListItmeViewCache = new View[7];
    private MemDateAdapter[] mListAdapterCache = new MemDateAdapter[7];
    private IdisplayNetErrAndRefreshView mNetView;
    
    public MemFlowViewAdapter(Context context)
    {
        super(context);
    }

    public void setIdisplayNetErrAndRefreshView(IdisplayNetErrAndRefreshView view)
    {
        this.mNetView = view;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        int pos = position % 7;
        if( mListItmeViewCache[pos] == null)
        {
            View view = inflater.inflate(R.layout.view_pull_listview, null);            
            PullToRefreshListView listview = (PullToRefreshListView)view.findViewById(R.id.listView);
            MemDateAdapter adapter = new MemDateAdapter(mContext, listview);
            adapter.setIdisplayNetErrAndRefreshView(mNetView);
            adapter.setAction((ShopDisCountListAction)getItem(pos));
            listview.setAdapter(adapter);           
            mListItmeViewCache[pos] = view;
            mListAdapterCache[pos]  = adapter;
            convertView = view;
        }else
        {
            convertView = mListItmeViewCache[pos];
        }

        return convertView;
    }

    public void setAreaId(int areaId)
    {
        for(MemDateAdapter one : mListAdapterCache )
        {
            if(one != null)
            {
                one.setAreaId(areaId);
            }
        }
    }
    
    public void loadData(int pos)
    {
        if(mListAdapterCache[pos] != null)
        {
            mListAdapterCache[pos].onReload();
        }
    }
}
